/**
 * 依赖包：jquery.min.js、jquery.eui.min.js、etb-collection.js
 */
$.extend($.fn.treegrid.methods, {
	/**
	 * 删除选中项
	 */
    deleteSelected : function(jq, param) {
        var treegrid = $(jq);
        var selections = treegrid.treegrid("getSelections");
        if(selections.length == 0) {
	    	$.messager.alert('提示', '请先选择要删除的内容', 'info');
	    	return;
	    }
    	var ids = new List();
    	new List(selections).each(function(selection){
    		ids.add(selection.id);
    	});
    	var json = {ids: ids.toString()};
    	$.messager.confirm('提示', '你确定要删除选中项？', function(result){
    		if(result) {
    			var parent = treegrid.treegrid('getParent',selections[0].id);
    			if(parent) {
    				treegrid.treegrid("clearSelections");
    				treegrid.treegrid('select',parent.id);
    			}
    			ajax({
    				url:param.url, 
    				data:json, 
    				success:function(data) {
    					if(param.success) {
    						param.success(data);
    					}
    					if(parent) {
    						treegrid.treegrid('reload',parent.id);
    					} else {
    						treegrid.treegrid('reloadWithCriteria');
    					}
    				}
    			});
    		}
    	});
    },
    /**
	 * 携带条件重新加载
	 */
    reloadWithCriteria : function(jq, param) {
        var treegrid = $(jq);
        var toolbar = treegrid.treegrid('options').toolbar;
    	if(toolbar) {
    		var form = new ListForm(toolbar);
    		var map = new Map(form.getData());
    		map.clearNullAndEmptyValue();
    		treegrid.treegrid("load", map.toJson());
    		treegrid.treegrid("clearSelections");
    	}
    },
    /**
	 * 重新加载父节点
	 */
    reloadParent : function(jq, param) {
        var treegrid = $(jq);
        var node = treegrid.treegrid('getSelected');
        treegrid.treegrid('unselect',node.id)
		node = treegrid.treegrid('getParent',node.id);
		if(node) {
			treegrid.treegrid('select',node.id)
			treegrid.treegrid('reload',node.id);
		} else {
			treegrid.treegrid('reloadWithCriteria');
		}
    },
    /**
	 * 重新加载当前选中节点
	 */
    reloadSelected : function(jq, param) {
        var treegrid = $(jq);
        var node = treegrid.treegrid('getSelected');
		if(node) {
			treegrid.treegrid('reload',node.id);
		} else {
			treegrid.treegrid('reloadWithCriteria');
		}
    },
    /**
	 * 取消所有选中行，只选中当前指定行
	 */
    onlySelectThisRow : function(jq, param) {
        var treegrid = $(jq);
        treegrid.treegrid('clearSelections');
        treegrid.treegrid('select',param.id);
    }
});
function onlySelectTreegridRow(row) {
	$(this).treegrid('onlySelectThisRow', row);
}